package com.smartlink.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.smartlink.pojo.Student;
import com.smartlink.pojo.Teacher;
import com.smartlink.service.LoginService;

public class LoginAction extends ActionSupport implements RequestAware, ServletResponseAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unused")
	private Map<String, Object> request;
	private HttpServletResponse response;
	private LoginService loginService;

	private String number;// 账号
	private String password;// 密码

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;

	}

	/**
	 * 学生、教师登录
	 */
	public void login() {

		if (number.length() < 9) {
			return;
		}

		// 判断是教师还是学生
		String post = judgment(number);
		if ("student".equals(post)) {
			// 学生验证
			Student student = loginService.studentLogin(number);
			if (student != null) {
				if (!student.getPasswrod().equals(password)) {
					// 密码错误
					student = null;
				}
				// 密码不正确返回空
				JSONObject jsonObject = new JSONObject();
				if (student == null) {
					jsonObject.put("student", "null");
				} else {
					JSONObject jsonStudent = new JSONObject(student);
					jsonObject.put("student", jsonStudent);
				}
				sendData(jsonObject.toString());
			} else {
				// 学号不正确
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("student", "null");
				sendData(jsonObject.toString());
			}
		} else {
			// 教师验证
			Teacher teacher = loginService.teacherLogin(number);
			if (teacher != null) {

				if (!teacher.getPassword().equals(password)) {
					teacher = null;
				}

				JSONObject jsonObject = new JSONObject();
				// 密码不正确
				if (teacher == null) {
					jsonObject.put("teacher", "null");
				} else {
					JSONObject jsonTeacher = new JSONObject(teacher);
					jsonObject.put("teacher", jsonTeacher);
				}

				sendData(jsonObject.toString());

			} else {
				// 工号不正确
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("teacher", "null");
				sendData(jsonObject.toString());
			}
		}

	}

	public void sendData(String data) {
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/json; charset=utf-8");
			out = response.getWriter();
			out.println(data);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	/**
	 * 根据账号判断是学生还是老师
	 * 
	 * @param number
	 * @return
	 */
	public String judgment(String number) {
		String str = number.substring(3, 6);
		if ("141".equals(str)) {
			return "student";
		} else {
			return "teacher";
		}
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}

}
